package com.beiding.workordersystem.main.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.beiding.workordersystem.main.utils.Json;
import lombok.Data;

@Data
public class OptionItem  {

    private String id;

    private String optionId;

    private String theKey;

    private String valueString;

    //值的类型
    private String valueType;

    @TableField(exist = false)
    private Object real;

    public void setValue(Object value) {
        if (value != null) {
            valueType = value.getClass().getName();
            try {
                this.valueString = Json.stringify(value);
                this.real = value;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    public Object getValue() {

        if (this.real != null) {
            return this.real;
        }

        if (valueType==null|| valueString == null) {
            return null;
        }
        try {
            Object o = Json.parse(this.valueString, Class.forName(valueType));
            this.real = o;
            return o;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
